package com.example.myapplication.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.myapplication.NewsInitActivity;
import com.example.myapplication.R;
import com.example.myapplication.bean.NewsListBean;
import com.example.myapplication.util.TagUtil;

import java.util.List;

public class NewsListAdapter extends RecyclerView.Adapter<NewsListAdapter.ViewHolder> {
    private List<NewsListBean> homeList1Beans;
    private Context context;
    public  NewsListAdapter(Context context, List<NewsListBean> homeList1Beans){
        this.homeList1Beans = homeList1Beans;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news_list,parent,false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.tvNewsTitle.setText(homeList1Beans.get(position).getTitle());
        holder.tvFrom.setText("来源：" + homeList1Beans.get(position).getAuthor_name());
        holder.tvDate.setText(homeList1Beans.get(position).getDate());

        //如果有一张缩略图
        if (homeList1Beans.get(position).getThumbnail_pic_s() != null){
            Glide.with(context).load(homeList1Beans.get(position).getThumbnail_pic_s()).into(holder.imgNews1);
        }else{
            //如果第一张缩略图为空
            holder.imgNews1.setVisibility(View.GONE);
            holder.imgNews2.setVisibility(View.GONE);
            holder.imgNews3.setVisibility(View.GONE);
            return;
        }

        //如果只有两张缩略图,显示最做左边和最右边的图
        if (homeList1Beans.get(position).getThumbnail_pic_s02() != null  && homeList1Beans.get(position).getThumbnail_pic_s03() == null){
            holder.imgNews2.setVisibility(View.GONE);
            holder.imgNews3.setVisibility(View.VISIBLE);
            Glide.with(context).load(homeList1Beans.get(position).getThumbnail_pic_s02()).into(holder.imgNews3);
        }else{
            holder.imgNews2.setVisibility(View.VISIBLE);
            holder.imgNews2.setVisibility(View.VISIBLE);
            holder.imgNews3.setVisibility(View.VISIBLE);
            //如果只有三张缩略图
            Glide.with(context).load(homeList1Beans.get(position).getThumbnail_pic_s02()).into(holder.imgNews2);
            if (homeList1Beans.get(position).getThumbnail_pic_s03() != null){
                Glide.with(context).load(homeList1Beans.get(position).getThumbnail_pic_s03()).into(holder.imgNews3);
            }
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, NewsInitActivity.class);
                intent.putExtra(TagUtil.NEWS_ID,homeList1Beans.get(position).getUniquekey());
                context.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return homeList1Beans.size();
    }

    public List<NewsListBean> getData(){
        return homeList1Beans;
    }

    //刷新数据
    public void setData(List<NewsListBean> datas){
        this.homeList1Beans=datas;
        notifyDataSetChanged();
    }

    //加载更多
    public void appendData(List<NewsListBean> newData) {
        int oldSize = homeList1Beans.size();
        homeList1Beans.addAll(newData);
        notifyItemRangeInserted(oldSize, newData.size());
    }
    public class ViewHolder extends RecyclerView.ViewHolder {
         TextView tvNewsTitle;
         LinearLayout lin1;
         ImageView imgNews1;
         ImageView imgNews2;
         ImageView imgNews3;
         TextView tvFrom;
         TextView tvDate;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvNewsTitle = (TextView) itemView.findViewById(R.id.tv_news_title);
            lin1 = (LinearLayout) itemView.findViewById(R.id.lin1);
            imgNews1 = (ImageView) itemView.findViewById(R.id.img_news1);
            imgNews2 = (ImageView) itemView.findViewById(R.id.img_news2);
            imgNews3 = (ImageView) itemView.findViewById(R.id.img_news3);
            tvFrom = (TextView) itemView.findViewById(R.id.tv_from);
            tvDate = (TextView) itemView.findViewById(R.id.tv_date);
        }
    }
}
